﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Data.SqlClient;
using MINERP.MODEL.FoundationModel;
using MINERP.COM;

namespace MINERP.DAL.NewPhotoRindGrid
{
    public class PhotoDAL_Grid
    {
        public List<PhotoInfo_Info> GetPhotoInfoByOrderProductId(int orderProductId, out int result)
        {
            result = 0;
            List<PhotoInfo_Info> photoList = new List<PhotoInfo_Info>();

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("GetPhotoInfo_GridByOrderProductId");
            db.AddInParameter(dbCommand, "orderProductId", DbType.Int32, orderProductId);

            try
            {
                using (IDataReader dr = db.ExecuteReader(dbCommand))
                {
                    while (dr.Read())
                    {
                        photoList.Add(new PhotoInfo_Info()
                        {
                            PhotoId = dr.GetInt32(0),
                            PhotoBarCode = dr.GetString(1),
                            PhotoName = dr.GetString(2),
                            Width = dr.GetDouble(3),
                            Height = dr.GetDouble(4),
                            Status = dr.GetInt32(5),
                            PicPath = dr.GetString(6),
                            PaperFaceName = dr.GetString(7),
                            PaperTypeName = dr.GetString(8),
                            PrinterName = dr.GetString(9),
                            IsRebackPhoto = Convert.ToInt32(dr["isReback"]) == (int)PhotoStatus2.innerReturn ? true : false
                        });
                    }
                }
            }
            catch (SqlException)
            {
                result = 2;//代表从数据库取数据出错
            }
            finally
            {
                if (dbCommand.Connection.State != ConnectionState.Closed)
                {
                    dbCommand.Connection.Close();
                }
            }

            return photoList;
        }

        public int GetPhotoInfo_InfoByBarCode(string strBarcode, out int photoId, out int OrderProductId, out int parentProductId)
        {

            int result = 0;//成功
            photoId = 0;
            OrderProductId = 0;
            parentProductId = 0;

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("GetPhotoInfoByBarCode");
            db.AddInParameter(dbCommand, "barCode", DbType.String, strBarcode);

            try
            {
                using (IDataReader dr = db.ExecuteReader(dbCommand))
                {
                    while (dr.Read())
                    {
                        photoId = dr.GetInt32(0);
                        OrderProductId = dr.GetInt32(1);
                        parentProductId = dr.GetInt32(2);
                        result = 1;
                    }
                }
            }
            catch (SqlException)
            {
                result = -2;//代表从数据库取数据出错
            }
            finally
            {
                if (dbCommand.Connection.State != ConnectionState.Closed)
                {
                    dbCommand.Connection.Close();
                }
            }

            return result;
        }

        /// <summary>
        /// 更新照片的状态
        /// </summary>
        /// <param name="photoId"></param>
        /// <param name="pStatus"></param>
        /// <returns></returns>
        public int UpPhotoStatus(int photoId, int pStatus)
        {
            int result = 1;

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UpPhotoStatus");
            db.AddInParameter(dbCommand, "PhotoId", DbType.Int32, photoId);
            db.AddInParameter(dbCommand, "pStatus", DbType.Int32, pStatus);

            try
            {
                db.ExecuteNonQuery(dbCommand);
            }
            catch (SqlException)
            {
                result = -2;//代表从数据库取数据出错
            }
            finally
            {
                if (dbCommand.Connection.State != ConnectionState.Closed)
                {
                    dbCommand.Connection.Close();
                }
            }

            return result;
        }

        /// <summary>
        /// 改变返单照片barcode的同时也改变了主产品对应照片和此照片的状态为分片完成
        /// </summary>
        /// <param name="PhotoId"></param>
        /// <param name="strBarCode"></param>
        /// <returns></returns>
        public int UpdateBarCode(int PhotoId, string strBarCode)
        {
            int result = 1;

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UpPhotoBarCode");
            db.AddInParameter(dbCommand, "photoid", DbType.Int32, PhotoId);
            db.AddInParameter(dbCommand, "barCode", DbType.String, strBarCode);

            try
            {
                db.ExecuteNonQuery(dbCommand);
            }
            catch (SqlException)
            {
                result = -2;//代表从数据库取数据出错
            }
            finally
            {
                if (dbCommand.Connection.State != ConnectionState.Closed)
                {
                    dbCommand.Connection.Close();
                }
            }

            return result;
        }
    }
}
